package com.example.chasshelper.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.chasshelper.EditInformationActivity;
import com.example.chasshelper.EditPasswordActivity;
import com.example.chasshelper.EditPhoneActivity;
import com.example.chasshelper.EditSettingActivity;
import com.example.chasshelper.R;
import com.example.chasshelper.adapter.ClassroomStuAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.DaoUtils;

public class SettingFragment extends Fragment implements View.OnClickListener {

    // TODO: Rename parameter arguments, choose names that match
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    private View view;
    private Activity mActivity;
    private Context mContext;

    //控件
    private ImageView iv_header;
    private TextView tv_userName;
    private TextView tv_userAccount;
    private RelativeLayout rl_changePassword;
    private RelativeLayout rl_changePhone;
    private RelativeLayout rl_setting;
    private RelativeLayout rl_logout;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //数据
    private Teacher teacher;
    private Student student;
    private String identity;
    //Bundle
    private Bundle bundle;

    public SettingFragment() {
    }

    // TODO: Rename and change types and number of parameters
    public static SettingFragment newInstance(String param1, String param2) {
        SettingFragment fragment = new SettingFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 绑定布局
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_setting, container, false);
        }
        initUI();
        initData();
        return view;
    }

    private void initUI() {
        //绑定控件
        iv_header = view.findViewById(R.id.iv_header);
        tv_userName = view.findViewById(R.id.tv_userName);
        tv_userAccount = view.findViewById(R.id.tv_userAccount);
        rl_changePassword = view.findViewById(R.id.rl_changePassword);
        rl_changePhone = view.findViewById(R.id.rl_changePhone);
        rl_setting = view.findViewById(R.id.rl_setting);
        rl_logout = view.findViewById(R.id.rl_logout);
        //点击事件
        iv_header.setOnClickListener(this);
        rl_changePassword.setOnClickListener(this);
        rl_changePhone.setOnClickListener(this);
        rl_setting.setOnClickListener(this);
        rl_logout.setOnClickListener(this);
    }

    //接收并传递数据
    private void initData() {
        //接收数据
        mActivity = getActivity();
        mContext = getContext();
        bundle = this.getArguments();
        identity = (String) bundle.getSerializable("identity");
        if (identity.equals("学生")) {
            student = (Student) bundle.getSerializable("student");
            //回显
            tv_userName.setText(student.getStudentName());
            tv_userAccount.setText(student.getStudentAccount());
        } else {
            teacher = (Teacher) bundle.getSerializable("teacher");
            //回显
            tv_userName.setText(teacher.getTeacherName());
            tv_userAccount.setText(teacher.getTeacherAccount());
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.putExtra("identity", identity);
        if (identity.equals("学生")) {
            intent.putExtra("student", student);
        } else {
            intent.putExtra("teacher", teacher);
        }
        switch (view.getId()) {
            case R.id.iv_header://跳转到个人信息编辑页面
                intent.setClass(mActivity, EditInformationActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_changePassword://跳转到修改密码页面
                intent.setClass(mActivity, EditPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_changePhone://跳转到修改手机号页面
                intent.setClass(mActivity, EditPhoneActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_setting://跳转到通用设置页面
                intent.setClass(mActivity, EditSettingActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_logout://返回登录页面
                mActivity.finish();
                break;
        }
    }
}